//
// Created by Administrator on 2021/10/20.
//
#include "anyList.h"
#include <iostream>
using namespace std;
void AnyList::insertBack(int num)
{
    Node *temp = ptrToFirst;
    Node *newNode = new Node;
    newNode->setData(num);
    newNode->setPtrToNext(nullptr);
    if (temp == nullptr) {
        ptrToFirst = newNode;
    } else {
        while (temp->getPtrToNext() != nullptr){
            temp = temp->getPtrToNext();
        }
        temp->setPtrToNext(newNode);
    }
    count++;
}

bool AnyList::search(int num) const
{
    if (ptrToFirst == nullptr) {
        cerr << "The list is empty." << endl;
        return false;
    }else {
        Node *temp = ptrToFirst;
        while (true){
            if (temp->getData() == num) {
                return true;
            } else {
                if (temp->getPtrToNext() == nullptr){
                    return false;
                } else {
                    temp = temp->getPtrToNext();
                }
            }
        }
    }
}

void AnyList::replaceData(int num1, int num2) {
    Node *temp = ptrToFirst;
    while (true) {
        if (temp != nullptr) {
            if (temp->getData() == num1) {
                temp->setData(num2);
                return;
            } else {
                temp = temp->getPtrToNext();
            }
        } else {
            break;
        }
    }
    cerr << "Element is not in the list." << endl;
}

void AnyList::deleteElem(int num)
{
    Node *temp = ptrToFirst;
    if (ptrToFirst == nullptr)
    {
        cerr << "The list is empty." << endl;
    }
    else
    {
        bool isDelete = false;
        if (ptrToFirst->getData() == num)
        {
            Node *tem = ptrToFirst;
            ptrToFirst = ptrToFirst->getPtrToNext();
            count--;
            delete tem;
            tem = nullptr;
            isDelete = true;
        }
        else
        {
            while (true)
            {
                if (temp->getPtrToNext() != nullptr)
                {
                    if (temp->getPtrToNext()->getData() == num)
                    {
                        Node *aim = temp->getPtrToNext();
                        Node *next = aim->getPtrToNext();
                        delete aim;
                        aim = nullptr;
                        temp->setPtrToNext(next);
                        isDelete = true;
                        count--;
                        break;
                    }
                    else
                    {
                        temp = temp->getPtrToNext();
                    }
                }
                else
                {
                    break;
                }
            }
        }
        if (!isDelete)
        {
            cerr << "Element " << num << " is not in the list." << endl;
        }
    }
}

int AnyList::getFirstElem() const
{
    return ptrToFirst->getData();
}

bool AnyList::isEmpty() const
{
    return ptrToFirst == nullptr;
}

void AnyList::deleteLastNode()
{
    if (ptrToFirst == nullptr)
    {
        cerr << "Cannot delete from an empty list." << endl;
    }
    else
    {
        Node *temp = ptrToFirst;
        if (temp->getPtrToNext() == nullptr)
        {
            ptrToFirst = nullptr;
            count = 0;
        }
        else
        {
            while (temp->getPtrToNext()->getPtrToNext() != nullptr)
            {
                temp = temp->getPtrToNext();
            }
            count--;
            Node *node = temp->getPtrToNext();
            temp->setPtrToNext(node->getPtrToNext());
            delete node;
            node = nullptr;
        }
    }
}

bool AnyList::compareLists(AnyList anyList) const
{
    if (count != anyList.count)
    {
        return false;
    }
    else
    {
        Node *node1 = ptrToFirst;
        Node *node2 = anyList.ptrToFirst;
        while (node1 != nullptr)
        {
            if (node1->getData() != node2->getData())
            {
                return false;
            }
            node1 = node1->getPtrToNext();
            node2 = node2->getPtrToNext();
        }
        return true;
    }
}

